package com.irdstudio.efp.esb.api.rest.wsd;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.api.bean.wsd.credit.req.finalcredit.NlsWsdFinalReqBean;
import com.irdstudio.efp.esb.api.bean.wsd.credit.resp.NlsWsdFirstRespBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.api.common.GroupeInterface;
import com.irdstudio.efp.esb.api.rest.EsbController;
import com.irdstudio.efp.esb.api.service.wsd.NlsWsdProcessServcie;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 需求编号:2019D0910
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2019/8/21
 * 功能描述:
 * 修改日期:2019/8/21
 * 修改描述:
 */
@RestController
@RequestMapping("/esb")
public class NlsDefaultController implements EsbController {
    /**
     * 日志信息
     */
    private final Logger logger = LoggerFactory.getLogger(NlsDefaultController.class);

    @PostMapping("/30220001_07_DEFAULT")
    public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult bindingResult1,
                                     @Validated({GroupeInterface.wsd.class}) @ModelAttribute("reqCreditBean") NlsWsdFinalReqBean reqWsdCreditBean, BindingResult bindingResult2, Exception exception) {
        // 授信申请流水号
        String crdtAppFlowNo = reqWsdCreditBean.getAdptCrdtAplNo();
        String requestId = reqWsdCreditBean.getRqsIdmptntID();


        logger.info("======>网商贷授信终审接口【" + crdtAppFlowNo + "_30220001_07】开始<======");
        // 参数校验
        try {
            EsbRespServiceBean esbRespServiceBean = nlsWsdProcessServcie.getEsbRespServiceBean(requestId +"_"+crdtAppFlowNo);
             if (esbRespServiceBean != null){
                return esbRespServiceBean;
            }
            validateBean(esbReqServiceBean, bindingResult1, reqWsdCreditBean, bindingResult2, exception, logger);
            // 设置系统头对象
            setSysHeadFieldThreadLocal(esbReqServiceBean);
            // 走到这里则不是正常返回
            logger.error("授信复审通知【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+" TrlRvwFlg 初审复审标志输入有误!!!");
            NlsWsdFirstRespBean failureRespBean= nlsWsdProcessServcie.getFailureRespBean(requestId, crdtAppFlowNo, EsbRetCodeStandard.YWLJCLYC.getValue(),"TrlRvwFlg 初审复审标志输入有误!!!");
            EsbRespServiceBean esbFailureBean = EsbRespServiceHandler.esbFailureHandler(esbReqServiceBean,failureRespBean, EsbRetCodeStandard.YWLJCLYC.getValue(),"TrlRvwFlg 初审复审标志输入有误!!!");
            //nlsWsdProcessServcie.setEsbRespServiceBean(requestId+"_"+crdtAppFlowNo,esbFailureBean);
            return esbFailureBean;
        } catch (ESBException e) {
            NlsWsdFirstRespBean failureRespBean = nlsWsdProcessServcie.getFailureRespBean(reqWsdCreditBean.getRqsIdmptntID(), reqWsdCreditBean.getAdptCrdtAplNo(), e.getErrorCode(), e.getMessage());
            EsbRespServiceBean esbRespServiceBean = EsbRespServiceHandler.esbFailureHandler(esbReqServiceBean, failureRespBean, e.getErrorCode(), e.getMessage());
            // 将信息放入redis
            //nlsWsdProcessServcie.setEsbRespServiceBean(reqWsdCreditBean.getRqsIdmptntID()+"_"+reqWsdCreditBean.getAdptCrdtAplNo(),esbRespServiceBean);
            return esbRespServiceBean;
        }
    }

    @ModelAttribute("esbReqServiceBean")
    public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
        logger.info("获取 ESB请求系统头和请求应用头对象!");
        EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
        return esbReqServiceBean;
    }
    @ModelAttribute("reqCreditBean")
    public NlsWsdFinalReqBean getReqEsbBodyBean(HttpServletRequest request) {
        logger.info("获取 ESB请求体！");
        String esbReqBody = (String) request.getAttribute("esbReqBody");
        return JSONObject.parseObject(esbReqBody, NlsWsdFinalReqBean.class);
    }
    /** 授信过程服务*/
    @Autowired
    private NlsWsdProcessServcie nlsWsdProcessServcie;
}
